package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.mapper.FarmerCorrectMapper;
import com.tz.farmer.model.FarmerCorrect;
import com.tz.farmer.model.FarmerNutrition;
import com.tz.farmer.service.CorrectService;
import com.tz.farmer.so.BaseSo;
import com.tz.farmer.so.FarmerCorrectSo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CorrectServiceImpl extends ServiceImpl<FarmerCorrectMapper, FarmerCorrect> implements CorrectService {
    private static Logger logger = LoggerFactory.getLogger(CorrectServiceImpl.class);


    @Autowired
    FarmerCorrectMapper farmerCorrectMapper;

    @Override
    public ResultEx getCorrectInfo(BaseSo baseSo) throws Exception{
        //辟谣助手信息
        LambdaQueryWrapper<FarmerCorrect> correctQueryWrapper = Wrappers.lambdaQuery();
        correctQueryWrapper.eq(FarmerCorrect::getProperty, baseSo.getFlag()).eq(FarmerCorrect::getIsSued,2).orderByDesc(FarmerCorrect::getReleaseTime);
        List<FarmerCorrect> farmerCorrects = farmerCorrectMapper.selectList(correctQueryWrapper);
        return ResultEx.builder().makeSuccessResult(farmerCorrects);
    }

    @Override
    public ResultEx getCorrectDetailInfo(FarmerCorrectSo farmerCorrectSo) throws Exception{
        //辟谣助手详情信息
        LambdaQueryWrapper<FarmerCorrect> correctQueryWrapper = Wrappers.lambdaQuery();
        correctQueryWrapper.eq(FarmerCorrect::getId, farmerCorrectSo.getId());
        FarmerCorrect farmerCorrect = farmerCorrectMapper.selectOne(correctQueryWrapper);
        //更新后台查看次数
        FarmerCorrect correct=FarmerCorrect.builder().selectNum(farmerCorrect.getSelectNum()+1).id(farmerCorrectSo.getId()).build();
        farmerCorrectMapper.updateById(correct);
        logger.info("更新查看次数完成===============");
        return ResultEx.builder().makeSuccessResult(farmerCorrect);
    }
}
